package svgedit.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import svgedit.gui.actions.DocPropAction;

/**
 * A custom JDialog for dealing with user entered document width and height
 * 
 * @author SARJ
 * 
 */
public class DocPropDialog extends JDialog
{
	private static final long serialVersionUID = 5404631399693040937L;
	private JPanel toolsPanel = new JPanel();
	private JLabel widthLabel = new JLabel("Width: ");
	private JLabel heightLabel = new JLabel("Height: ");
	private JTextField widthField = new JTextField();
	private JTextField heightField = new JTextField();
	private JButton applyBtn = new JButton("Apply");
	private JButton cancelBtn = new JButton("Cancel");

	/**
	 * Construct a DocPropDialog object
	 */
	public DocPropDialog()
	{
		// Modal
		super(Frame.getFrame(), true);

		// add actions to the buttons
		applyBtn.addActionListener(new DocPropAction(this));
		cancelBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
			}
		});

		// toolsPanel settings
		toolsPanel.setLayout(new GridLayout(0, 2));
		toolsPanel.setSize(300, 300);
		toolsPanel.setVisible(true);

		// already include the current height / width
		heightField.setText(Frame.getFrame().getDocument().getHeight()
				.valueAsString());
		widthField.setText(Frame.getFrame().getDocument().getWidth()
				.valueAsString());

		// add the components to the DocPropDialog
		toolsPanel.add(widthLabel);
		toolsPanel.add(widthField);
		toolsPanel.add(heightLabel);
		toolsPanel.add(heightField);
		toolsPanel.add(cancelBtn);
		toolsPanel.add(applyBtn);
		this.add(toolsPanel);

		// DocPropDialog settings
		this.setLocationRelativeTo(null);
		this.setSize(200, 100);
		this.setVisible(true);
	}

	/**
	 * Gets the width value from the width JTextField
	 * 
	 * @return Width
	 */
	public int getUserWidth()
	{
		Double d = Double.parseDouble(widthField.getText());
		return d.intValue();
		// return Integer.parseInt(widthField.getText());
	}

	/**
	 * gets the height value from the height JTextField
	 * 
	 * @return Height
	 */
	public int getUserHeight()
	{
		Double d = Double.parseDouble(heightField.getText());
		return d.intValue();
		// return Integer.parseInt(heightField.getText());
	}
}
